import * as api from "./api";
import utils from "./utils";
import { mapActions } from 'vuex'
export const mymixin = {
    name: 'myNav',
    data() {
        return {
            path: this.$route.path,
            nowTime: '',
            newsCount: 0,
            isShow: true
        };
    }, 
    created () { 
        this.userid=this.$store.state.msgCount.user.user_id;
        this.getmsg()
    },
    updated() {
        this.show()
        // this.getmsg() 
    },
    mounted() {
        this.nowTimes();
    },
    beforeDestroy() {
        // 每次离开当前界面时，清除定时器
        this.clear();
    },
    methods: {
        ...mapActions(['outlogin']),
        getmsg() {
            this.axios({
                url: api.GET_QUERYUNREDINFO,
                method: 'get',
                params: {
                    user_id: 3/* 动态获取 */
                }
            }).then(data => {
                // console.log(data)
                if (data.data.data == null) {
                    this.newsCount = 0
                } else {
                    this.newsCount=data.data.data.length
                }
            })
        },
        /* 消息按钮消失出现 */
        show() {
            this.newsCount >= 1 ? this.isShow = false : this.isShow = true
        },
        //退出登录
        tuichu() {
            this.axios({
                url: api.GET_EXIT,
                method: 'get',
                params: {

                }
            }).then(data => {
                // console.log(data)
                if (data.data.code === 200){
                    this.outlogin()
                    utils.removeStorage('user')
                    this.$message({
                        type: 'success',
                        message: '成功退出!'
                    });
                    this.$router.push("/login");
                }
            })
        },
        timeFormate(timeStamp) {
            let year = new Date(timeStamp).getFullYear();
            let month = new Date(timeStamp).getMonth() + 1 < 10 ? "0" + (new Date(timeStamp).getMonth() + 1) :
                new Date(timeStamp).getMonth() + 1;
            let date = new Date(timeStamp).getDate() < 10 ? "0" + new Date(timeStamp).getDate() : new Date(
                timeStamp).getDate();
            let hh = new Date(timeStamp).getHours() < 10 ? "0" + new Date(timeStamp).getHours() : new Date(
                timeStamp).getHours();
            let mm = new Date(timeStamp).getMinutes() < 10 ? "0" + new Date(timeStamp).getMinutes() : new Date(
                timeStamp).getMinutes();
            let ss = new Date(timeStamp).getSeconds() < 10 ? "0" + new Date(timeStamp).getSeconds() : new Date(
                timeStamp).getSeconds();
            this.nowTime = year + "." + month + "." + date + "  " + "星期" + '日一二三四五六'.charAt(new Date().getDay()) +
                " " + hh + ":" + mm + ':' + ss;
        },
        nowTimes() {
            this.timeFormate(new Date());
            setInterval(this.nowTimes, 1000);
            this.clear()
        },
        clear() {
            clearInterval(this.nowTimes)
            this.nowTimes = null;
        }
    }
}